<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>待办任务列表</title>
</head>
<body>
	<div id="app">
		<div>
			<input type="text" v-model="item">
			<button @click="handleClick">Add</button>
		</div>
		<button @click=sortBy>倒序</button>
		<hr>
		<ul v-if="list.length > 0">
			<li v-for="(item, index) of list" :key="item.id">
				<span>{{ item.text }}</span>
				<!-- <button @click="handleDelete(index)"><i>&times;</i></button> -->
				<button @click="handleDelete(item.id)"><i>&times;</i></button>
			</li>
		</ul>
		<p v-else>暂无数据</p>
	</div>
	
	<script src="vue.js"></script>
	<script>
		
		const app = new Vue({
			data: function() {
				return {
					item: '',
					list: [
						{ id: 123, text: '吃饭' },
						{ id: 13, text: '睡觉' },
						{ id: 23, text: '打豆豆' },
						{ id: 1123, text: '写代码' },
					]
				}
			},
			methods: {
				handleDelete(id) {
					// console.log(id)
					// id, [].filter
					this.list = this.list.filter((item) => item.id != id)
					
					// console.log(ind)
					// index, [].splice()
					// this.list.splice(ind, 0)
					// console.log(this) // id, index
				},
				handleClick() {
					this.list.push({
						id: Date.now(),
						text: this.item
					})
					this.item = ''
					// console.log(this.item)
				},
				sortBy() {
					this.list.reverse()
				}
			}
		}).$mount("#app")
	</script>
</body>
</html>